package ajjj.kiviq;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Filters, forwards and chains incoming requests
 * to ensure that users are logged on before
 * accessing private files, etc
 * 
 * 
 * @author orjohan
 */
@WebFilter(filterName = "AuthFilter", urlPatterns = {"/*"})
public class AuthFilter implements Filter {
    
    public AuthFilter() {
    }

    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doFilter(ServletRequest srequest, ServletResponse sresponse,
            FilterChain chain)
            throws IOException, ServletException {
        
	//We'll be using Http requests
        HttpServletRequest request = (HttpServletRequest) srequest;
        HttpServletResponse response = (HttpServletResponse) sresponse;
        
        boolean loggedOn = request.getSession().getAttribute(Constants.User) != null;
        
        String req = getRequestedFile(request);
        String forward;
        
        Boolean isPublic = Constants.isPublic(req);
        if (isPublic == null) {	    //Public resource
            chain.doFilter(srequest, sresponse);
            return;
        }
        
	String originalReq = req;
        if (isPublic == true) {
            if (req.trim().isEmpty()) {
                forward = "login.jsp";
            } else {
                forward = req + ".jsp";
            }
        } else if (loggedOn) {
            String ext = ".jsf";
            if (!req.endsWith(ext))
                req += ext;
            forward = /*"WEB-INF/pages/" +*/ req;
        } else {
            response.sendRedirect(Constants.BasePath + "login");
            return;
        }
        System.out.println("REQUEST: " + req + ", FORWARD: " + forward);
	if (originalReq.equals(forward))
	    chain.doFilter(srequest, sresponse);
	else
	    request.getRequestDispatcher(forward).forward(request, response);
    }
    
    /**
     * Gets the requested file, relative to the
     * base path of the application
     * 
     * @param req The HttpServletRequest for this request
     * @return The relative request path
     */
    private String getRequestedFile(HttpServletRequest req) {
        String ret = req.getRequestURI();
        if (ret.indexOf(Constants.BasePath) == 0) {
            return ret.substring(Constants.BasePath.length());
        }
        return ret;
    }
    
    @Override
    public void destroy() {
    }
    
    @Override
    public void init(FilterConfig fc) throws ServletException {
    }
}
